<?php 
require_once('class.db.php');
class trangchu extends db {
	function config(){
		$qr = "SELECT * FROM `config`";
		return mysql_query($qr);
	}
	function html($domain){
		$str = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>';
		$str .= "<base href=\"http://{$domain}/\" />";
		$str .= "<meta http-equiv=\"content-language\" content =\"vi\" />";
		$str .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
		echo $str;
	}
	function seo($domain,$title,$description,$keywords,$image,$url){
		$str = "<title>{$title}</title>
		<meta name=\"description\" content=\"{$description}\" />
		<meta name=\"keywords\" content=\"{$keywords}\" />
		<meta name=\"title\" content=\"{$title}\" />
		<meta name=\"robots\" content=\"NOODP\" />
		<meta name=\"viewport\" content=\"width=1024px\" />
		<meta property=\"og:title\" content=\"{$title}\" />
		<meta property=\"og:image\" content=\"http://{$domain}{$image}\" />
		<meta property=\"og:site_name\" content=\"{$domain}\" />
		<meta property=\"og:url\" content=\"http://{$domain}{$url}\" />
		<meta property=\"og:description\" content=\"{$description}\" />";
		echo $str;
	}
	function css($css){
		for($i=0; $i<=count($css); $i++) $str .= $css[$i];
		$str .= '</head><body>';
		echo $str;
	}
	function script($script){
		for($i=0; $i<=count($script); $i++) $str .= $script[$i];
		echo $str;
	}
	function menu_one($id){
		$qr = "SELECT id,name,name_rewrite,url,url_hinh,title,metaDescription,metaKeyword,type_id FROM menu WHERE `delete`=0 AND status=1 AND id='{$id}'";
		return mysql_query($qr);
	}
	function menu($level,$position){
		$qr = "SELECT id,name,url FROM menu WHERE `delete`=0 AND status=1 AND position_id LIKE '%,{$position},%' AND parent_id = {$level} ORDER BY `order`";
		return mysql_query($qr);
	}
	function getSubmenu($level,$position){
		$sql = $this->menu($level,$position);
		if(mysql_num_rows($sql) > 0){
			echo '<ul>';
			while($row = mysql_fetch_array($sql)){
				echo "<li><a href=\"{$row['url']}\" >{$row['name']}</a>";
				$this->getSubmenu($row['id'],$position);
				echo '</li>';
			}
			mysql_free_result($sql);
			echo '</ul>';
		}
	}
	function navigator($id){
		$qr = $this->menu_one($id);
		$row = mysql_fetch_array($qr);
		mysql_free_result($qr);
		$str = "<div class=\"title\">{$row['name']}</div>";
		return $str;
	}
	function list_item($select,$table,$where,$limit=NULL){
		$qr = "SELECT {$select} FROM {$table} WHERE {$where} ORDER BY date_update DESC {$limit}";
		return mysql_query($qr);
	}
	function link_detail($menu_id){
		$str = explode(',',$menu_id);
		$i = count($str)-2;
		$m = $this->menu_one($str[$i]);
		$row_m = mysql_fetch_array($m);
		return $row_m['url'];
	}
	function phantrang($link,$page_number,$max_results,$select,$table,$where,$keyword=NULL,$catalog=NULL,$order=NULL){
		$qr = $this->list_item($select,$table,$where);
		$total = mysql_num_rows($qr);
		mysql_free_result($qr);
		
		$str = '<div style="clear:both"></div>';
		$total_pages = ceil($total / $max_results);
		if($total_pages > 1){
			if($page_number > 1){
				$prev = ($page_number - 1);
				$str .= "<a href='{$link}{$prev}/'>Trang trước</a>";
			}
			for($i = $page_number - 5; $i <= $page_number + 5 & $i <= $total_pages; $i++){
				if($page_number == $i){
					$str .= "<a href='{$link}{$i}/' style='color:#F00; font-weight:bold; background-color:#ffffcc'>{$i}</a>";
				} elseif($i>0) {
					$str .= "<a href='{$link}{$i}/'>{$i}</a>";
				}
			}
			if($page_number < $total_pages){
				$next = ($page_number + 1);
				$str .= "<a href='{$link}{$next}/'>Trang sau</a>";
			}
		}
		return $str;
	}
	function detail($id){
		$qr = "SELECT id,name,name_rewrite,url_hinh,description,content,date_create,metaKeyword FROM info WHERE `delete`=0 AND status=1 AND id='{$id}'";
		return mysql_query($qr);
	}
	function guiykien($table,$name,$email,$content,$id=NULL){
		$date = date('Y-m-d H:i:s');
		if($table=='info_comment') $qr = "INSERT INTO {$table} (`date_create`,`date_update`,`user_create`,`content`,`email`,`name`,`info_id`) VALUES ('{$date}','{$date}','khachhang','{$content}','{$email}','{$name}','{$id}')";
		else if($table=='question') $qr = "INSERT INTO {$table} (`date_create`,`date_update`,`user_create`,`content`,`email`,`name`) VALUES ('{$date}','{$date}','khachhang','{$content}','{$email}','{$name}')";
		else if($table=='answers') $qr = "INSERT INTO {$table} (`date_create`,`date_update`,`user_create`,`question_id`,`content`,`email`,`name`) VALUES ('{$date}','{$date}','khachhang','".$_SESSION['cauhoi']."','{$content}','{$email}','{$name}')";
		mysql_query($qr);
	}
	function muaban(){
		$menu_id = ',15,';
		$date = date('Y-m-d H:i:s');
		$other2 = "{$_POST['FullName']} | {$_POST['Email']} | {$_POST['Address']} | {$_POST['Tel']}";
		$alias = $this->change_alias($_POST['Title']);
		$qr = "INSERT INTO info VALUES (null,'{$_POST['Title']}','{$alias}','','','{$_POST['Comment']}' ,'{$date}','{$date}','0','','0','{$other2}','vi','{$menu_id}','khachhang','','0')";
		mysql_query($qr);
	}
	// GET link
	function id_info($alias){
		$qr = "SELECT id FROM info WHERE `delete`=0 AND status=1 AND name_rewrite='$alias'";
		$row = mysql_fetch_array(mysql_query($qr));
		return $row['id'];
	}
	function id_video($alias){
		$qr = "SELECT id FROM video WHERE `delete`=0 AND status=1 AND name_rewrite='$alias'";
		$row = mysql_fetch_array(mysql_query($qr));
		return $row['id'];
	}
	function id_menu($alias){
		$qr = "SELECT id FROM menu WHERE `delete`=0 AND status=1 AND name_rewrite='$alias'";
		$row = mysql_fetch_array(mysql_query($qr));
		return $row['id'];
	}
	
	function change_alias($alias){
		if(!$alias) return false;
		$str = $alias;
		$unicode = array(
			'a'=>'á|à|ả|ã|ạ|ă|ắ|ằ|ẳ|ẵ|ặ|â|ấ|ầ|ẩ|ẫ|ậ',
			'A'=>'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ằ|Ẳ|Ẵ|Ặ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
			'd'=>'đ',
			'D'=>'Đ',
			'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ',
			'E'=>'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
			'i'=>'í|ì|ỉ|ĩ|ị',	  
			'I'=>'Í|Ì|Ỉ|Ĩ|Ị',
			'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ',
			'O'=>'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
			'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự',
			'U'=>'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
			'y'=>'ý|ỳ|ỷ|ỹ|ỵ',
			'Y'=>'Ý|Ỳ|Ỷ|Ỹ|Ỵ',
			'' =>"`|~|!|@|#|$|%|^|&|*|(|)|-|_|+|=|\|[|]|{|}|:|;|\"|'|,|.|/|<|>|?|“|”|  ",
			'-'=>"| "
		);
		foreach($unicode as $khongdau=>$codau) {
			$arr=explode("|",$codau);
			$str = str_replace($arr,$khongdau,$str);
		}
		return strtolower($str);
	}
	
	
}// end trangchu